<?php
$page = 'admin';
include '../database/connect.php';
$question_id = $_GET['question_id'];

function addAnswer($questionId, $answer, $correct, $added_by) {
    $query = "INSERT INTO answers SET question_id='$questionId',answer='$answer',correct='$correct', added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
}

if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    addAnswer($question_id, $a, isset($acheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $b, isset($bcheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $c, isset($ccheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $d, isset($dcheck) ? 1 : 0, $added_by);
    header("Location: ?question_id=$question_id");
}


if (isset($_GET['update'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "DELETE FROM answers WHERE question_id='$question_id'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    addAnswer($question_id, $A, isset($Acheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $B, isset($Bcheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $C, isset($Ccheck) ? 1 : 0, $added_by);
    addAnswer($question_id, $D, isset($Dcheck) ? 1 : 0, $added_by);
    header("Location: ?question_id=$question_id");
}

require_once 'header.php';
?>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#answers_form").validationEngine();
    });
</script>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#update_answers_form").validationEngine();
    });
</script>
<!--display the question-->
<h3 style="padding-left: 15px">Question:</h3>
<p style="padding-left: 20px">
    <?php
    $query = "SELECT * FROM questions where question_id='$question_id'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $row = mysql_fetch_assoc($result);
    $content = $row['content'];
    $quiz_id = $row['quiz_id'];

    $query = "select * from quizes where quiz_id='$quiz_id'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $row = mysql_fetch_assoc($result);
    $course_id = $row['course_id'];
    $quiz = $row['description'];

//lets get the course name for purpose of navigation
    $query = "SELECT * FROM courses where id='$course_id'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $row = mysql_fetch_assoc($result);
    $course = $row['course'];
    ?>
</p>


<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;
    <a href="lessons.php?course_id=<?php echo $course_id; ?>"><?php echo $course; ?></a>&gt;&gt;
    <a href="questions.php?quiz_id=<?php echo $quiz_id; ?>"><?php echo $quiz; ?></a>
</p>

<?php echo $content; ?>
    <!--display the answers-->
<?php
    $answerLabel = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $query = "SELECT * FROM answers where question_id='$question_id' order by answers_id asc";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    if (mysql_num_rows($result) == 0) {
?>
        <form action="?save=1" method="POST" id="answers_form">
            <input type="hidden" name="question_id" value="<?php echo $_GET['question_id']; ?>" />
            <table>
                <tr><td style="width: 2%">A:</td>
                    <td><textarea style="width: 90%" class="validate[required] wide" name="a" id='a'></textarea></td>
                    <td><input type="checkbox" name="acheck" value="1" /></td>
                </tr>
                <tr><td>B:</td>
                    <td><textarea style="width: 90%" class="validate[required] wide" name="b" id='b'></textarea></td>
                    <td><input type="checkbox" name="bcheck" value="1" /></td>
                </tr>
                <tr><td>C:</td>
                    <td><textarea style="width: 90%" class="validate[required] wide" name="c" id='c'></textarea></td>
                    <td><input type="checkbox" name="ccheck" value="1" /></td>
                </tr>
                <tr><td>D:</td>
                    <td><textarea style="width: 90%" class="validate[required] wide" name="d" id='d'></textarea></td>
                    <td><input type="checkbox" name="dcheck" value="1" /></td>
                </tr>
                <td colspan="2" style="text-align: right;"><input type="submit" value="Save Answers"/></td>
            </table>
        </form>
<?php
    } else {
?>

        <form action="?update=1" method="POST" id="update_answers_form">
            <input type="hidden" name="question_id" value="<?php echo $_GET['question_id']; ?>" />
            <table>
                <tr style="text-align: left">
                    <th></th>
                    <th>
                        Answer
                    </th>
                    <th>
                        Correct?
                    </th>
                </tr>

        <?php
        $count = 0;
        while ($row = mysql_fetch_assoc($result)) {
            //var_dump($row);
            extract($row);
        ?>
            <tr><td style="width: 2%"><?php echo $answerLabel{$count}; ?>:</td>
                <td><textarea style="width: 90%" class="validate[required] wide" name="<?php echo $answerLabel{$count}; ?>"
                              id='<?php echo $answerLabel{$count}; ?>'><?php echo $answer; ?></textarea>
                </td>
                <td><input type="checkbox" name="<?php echo $answerLabel{$count}; ?>check" id="<?php echo $answerLabel{$count}; ?>check" value="<?php echo $correct; ?>"
                       <?php if ($correct == 1) {
 ?>checked="checked" <?php } ?> />
            </td>
        </tr>
        <?php
                       $count++;
                   }
        ?>
                   <td colspan="3" style="text-align: right;"><input type="submit" value="Update Answers"/></td>
               </table>
           </form>

<?php
               }
?>


<?php require_once('footer.php'); ?>